<?php

/* 提现记录 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;
use app\admin\controller\WithdrawController as Withdraw;
class CashController extends AdminBaseController
{
    var $status=array("0"=>"未处理","1"=>"已完成","2"=>"已拒绝","3"=>"提现中");
    var $type=array(
        '1'=>'支付宝',
        '2'=>'微信',
        '3'=>'银行卡',
    );
        
    public function index()
    {
        
        $data = $this->request->param();
        $map=[];		
		$status=isset($data['status']) ? $data['status']: '';
        if($status!=''){
            $map[]=['status','=',$status];
        }
		
        $start_time=isset($data['start_time']) ? $data['start_time']: '';
        $end_time=isset($data['end_time']) ? $data['end_time']: '';
        if($start_time!=""){
           $map[]=['addtime','>=',strtotime($start_time)];
        }
        if($end_time!=""){
           $map[]=['addtime','<=',strtotime($end_time) + 60*60*24];
        }

        $uid=isset($data['uid']) ? $data['uid']: '';
        if($uid!=''){
            $map[]=['uid','=',$uid];
        }
		
		
		$keyword=isset($data['keyword']) ? $data['keyword']: '';
        if($keyword!=''){
            $map[]=['orderno|trade_no','like',"%".$keyword."%"];
        }

        
        $list = Db::name('cash_record')
            ->where($map)
            ->order("id desc")
            ->paginate(20);
        $list->each(function($v,$k){
           $v['userinfo']= getUserInfo($v['uid']);
           return $v; 
        });
        
        $list->appends($data);
        
        $page = $list->render();
        $this->assign("page", $page);
		
		$cashrecord_total = Db::name("cash_record")->where($map)->sum("money");
		$cash['type'] = 1;
        if($status=='')
        {
            $success=$map;
            $success[]=['status','=',1];
			$pending=$map;
            $pending[]=['status','=',0];
            $fail=$map;
            $fail[]=['status','=',2];
            $cashrecord_success = Db::name("cash_record")->where($success)->sum("money");
            $cashrecord_fail = Db::name("cash_record")->where($fail)->sum("money");
			$cashrecord_pending = Db::name("cash_record")->where($pending)->sum("money");
            $cash['success']=$cashrecord_success;
            $cash['fail']=$cashrecord_fail;
			$cash['pending']=$cashrecord_pending;
            $cash['type']=0;
        }
        $cash['total']=$cashrecord_total;
    	$this->assign('cash', $cash);
            
        $this->assign('list', $list);
        $this->assign('status', $this->status);
        $this->assign('type', $this->type);

        return $this->fetch();
    }


    public function setCash()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        if(!$id){
            $this->error("数据传入失败！");
        }
        
        $status = $this->request->param('status');
        
        $result=Db::name("cash_record")->where("id={$id}")->find();
        if($result){
            if($result['status']!=0){
                $this->error("该订单已处理");
            }

            if($status==2){
                try {
                    Db::startTrans();
                    Db::name("user")->where("id='{$result['uid']}'")->inc("votes",$result['votes'])->update();
                    Db::name("cash_record")->where("id='{$id}'")->update(array('status'=>2));
                    Db::commit();
                }catch (PDOException $e) {
                    Db::rollback();
                    $this->error('数据库错误', $e->getMessage());
                }
                $this->success('操作成功');
            }
            
            $update = Db::name("cash_record")->where("id='{$result['id']}'")->update(array("status"=>3,"uptime"=>time()));
            if($update){
                $withdraw = new Withdraw();
                $rs = $withdraw->MaxApply($result);
                if($rs['code'] ==0){
                    Db::name("cash_record")->where("id='{$id}'")->update(array("status"=>1,"uptime"=>time()));
                    $this->success('操作成功');    
                }else{
                    Db::name("cash_record")->where("id='{$id}'")->update(array("status"=>0,"uptime"=>time()));
                    $this->error("{$rs['results']}");
                }
            }

            $this->success('操作成功');
         }else{
            $this->error('数据传入失败！');
         }	
             
    }
}